﻿
namespace Imtihan.WebBk
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the ImtihanEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class ImtihanDomainServiceBk : LinqToEntitiesDomainService<ImtihanEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Assessments' query.
        public IQueryable<Assessment> GetAssessments()
        {
            return this.ObjectContext.Assessments;
        }

        public void InsertAssessment(Assessment assessment)
        {
            if ((assessment.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(assessment, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Assessments.AddObject(assessment);
            }
        }

        public void UpdateAssessment(Assessment currentAssessment)
        {
            this.ObjectContext.Assessments.AttachAsModified(currentAssessment, this.ChangeSet.GetOriginal(currentAssessment));
        }

        public void DeleteAssessment(Assessment assessment)
        {
            if ((assessment.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Assessments.Attach(assessment);
            }
            this.ObjectContext.Assessments.DeleteObject(assessment);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'AssessmentSchedules' query.
        public IQueryable<AssessmentSchedule> GetAssessmentSchedules()
        {
            return this.ObjectContext.AssessmentSchedules;
        }

        /// <summary>
        /// Get assessment schedules by passing assessment id.
        /// </summary>
        /// <param name="assessmentId">assessment id.</param>
        /// <returns>list of assessment schedule.</returns>
        [Invoke]
        public List<AssessmentSchedule> GetAssessmentSchedulesByAssessmentId(Guid assessmentId)
        {
            return this.ObjectContext.AssessmentSchedules.Where(aSched => aSched.AssessmentId == assessmentId).ToList();
        }

        /// <summary>
        /// Get assessment schedules by passing assessment id.
        /// </summary>
        /// <param name="assessmentId">assessment id.</param>
        /// <returns>list of assessment schedule.</returns>
        [Invoke]
        public AssessmentSchedule GetAssessmentScheduleByAssessmentId(Guid assessmentScheduleId)
        {
            return this.ObjectContext.AssessmentSchedules.Where(aSched => aSched.Id == assessmentScheduleId).FirstOrDefault();
        }

        public void InsertAssessmentSchedule(AssessmentSchedule assessmentSchedule)
        {
            if ((assessmentSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(assessmentSchedule, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AssessmentSchedules.AddObject(assessmentSchedule);
            }
        }

        public void UpdateAssessmentSchedule(AssessmentSchedule currentAssessmentSchedule)
        {
            this.ObjectContext.AssessmentSchedules.AttachAsModified(currentAssessmentSchedule, this.ChangeSet.GetOriginal(currentAssessmentSchedule));
        }

        public void DeleteAssessmentSchedule(AssessmentSchedule assessmentSchedule)
        {
            if ((assessmentSchedule.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AssessmentSchedules.Attach(assessmentSchedule);
            }
            this.ObjectContext.AssessmentSchedules.DeleteObject(assessmentSchedule);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Users' query.
        public IQueryable<User> GetUsers()
        {
            return this.ObjectContext.Users;
        }

        /// <summary>
        /// Get a user by passing a user's id
        /// </summary>
        /// <param name="id">user's id</param>
        /// <returns>user object</returns>
        [Invoke]
        public User GetUserById(string id)
        {
            return this.ObjectContext.Users.Where(u => u.Id.ToLower() == id.ToLower()).FirstOrDefault();
        }
        
        public void InsertUser(User user)
        {
            if ((user.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Users.AddObject(user);
            }
        }

        public void UpdateUser(User currentUser)
        {
            this.ObjectContext.Users.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
        }

        public void DeleteUser(User user)
        {
            if ((user.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Users.Attach(user);
            }
            this.ObjectContext.Users.DeleteObject(user);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'UserAssessmentSchedules' query.
        public IQueryable<UserAssessmentSchedule> GetUserAssessmentSchedules()
        {
            return this.ObjectContext.UserAssessmentSchedules;
        }

        /// <summary>
        /// Get user assessment schedules by passing assessment schedule id.
        /// </summary>
        /// <param name="assessmentScheduleId">assessment schedule id.</param>
        /// <returns>List of user assessment schedule for specific assessment schedule.</returns>
        [Invoke]
        public List<UserAssessmentSchedule> GetUserAssessmentSchedulesByAssessmentScheduleId(Guid assessmentScheduleId)
        {
            return this.ObjectContext.UserAssessmentSchedules.Where(uaSched => uaSched.AssessmentScheduleId == assessmentScheduleId).ToList();
        }

        /// <summary>
        /// Get user assessment schedule by passing assessment schedule id.
        /// </summary>
        /// <param name="assessmentScheduleId">assessment schedule id.</param>
        /// <returns>User assessment schedule for specific assessment schedule.</returns>
        [Invoke]
        public UserAssessmentSchedule GetUserAssessmentScheduleByAssessmentScheduleIdAndUserId(Guid assessmentScheduleId, string userId)
        {
            return this.ObjectContext.UserAssessmentSchedules.Include("AssessmentSchedule").Include("User").Where(uaSched => uaSched.AssessmentScheduleId 
                == assessmentScheduleId && uaSched.UserId.ToLower() == userId.ToLower()).FirstOrDefault();
        }

        public void InsertUserAssessmentSchedule(UserAssessmentSchedule userAssessmentSchedule)
        {
            if ((userAssessmentSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(userAssessmentSchedule, EntityState.Added);
            }
            else
            {
                this.ObjectContext.UserAssessmentSchedules.AddObject(userAssessmentSchedule);
            }
        }

        public void UpdateUserAssessmentSchedule(UserAssessmentSchedule currentUserAssessmentSchedule)
        {
            this.ObjectContext.UserAssessmentSchedules.AttachAsModified(currentUserAssessmentSchedule, this.ChangeSet.GetOriginal(currentUserAssessmentSchedule));
        }

        public void DeleteUserAssessmentSchedule(UserAssessmentSchedule userAssessmentSchedule)
        {
            if ((userAssessmentSchedule.EntityState == EntityState.Detached))
            {
                this.ObjectContext.UserAssessmentSchedules.Attach(userAssessmentSchedule);
            }
            this.ObjectContext.UserAssessmentSchedules.DeleteObject(userAssessmentSchedule);
        }
    }
}


